package com.abc.proxy.dynamic;

import com.abc.proxy.statics.IStudentDao;

import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class StudentProxy implements InvocationHandler {

   private IStudentDao studentDao;

   public StudentProxy(IStudentDao studentDao) {
      this.studentDao = studentDao;
   }

   @Override
   public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
      System.out.println("事务开启");
      Object obj = method.invoke(studentDao, args);
      System.out.println("事务关闭");
      return obj;
   }

   public Object newInstance() {
      return Proxy.newProxyInstance(studentDao.getClass().getClassLoader(), studentDao.getClass().getInterfaces(), this);
   }

}
